﻿using System;
using System.Collections.Generic;
using GeoAPI.Geometries;

namespace NetTopologySuite.Triangulate
{
	public class VertexTaggedGeometryDataMapper
	{
		public void LoadSourceGeometries(IGeometry geoms)
		{
			for (int i = 0; i < geoms.NumGeometries; i++)
			{
				IGeometry geometryN = geoms.GetGeometryN(i);
				this.LoadVertices(geometryN.Coordinates, geometryN.UserData);
			}
		}

		public void LoadSourceGeometries(ICollection<IGeometry> geoms)
		{
			foreach (IGeometry geometry in geoms)
			{
				this.LoadVertices(geometry.Coordinates, geometry.UserData);
			}
		}

		public void LoadSourceGeometries(IGeometryCollection geomColl)
		{
			for (int i = 0; i < geomColl.NumGeometries; i++)
			{
				IGeometry geometryN = geomColl.GetGeometryN(i);
				this.LoadVertices(geometryN.Coordinates, geometryN.UserData);
			}
		}

		private void LoadVertices(Coordinate[] pts, object data)
		{
			for (int i = 0; i < pts.Length; i++)
			{
				this._coordDataMap.Add(pts[i], data);
			}
		}

		public IList<Coordinate> Coordinates
		{
			get
			{
				return new List<Coordinate>(this._coordDataMap.Keys);
			}
		}

		public void TransferData(IGeometry targetGeom)
		{
			for (int i = 0; i < targetGeom.NumGeometries; i++)
			{
				IGeometry geometryN = targetGeom.GetGeometryN(i);
				Coordinate coordinate = (Coordinate)geometryN.UserData;
				if (coordinate != null)
				{
					geometryN.UserData = this._coordDataMap[coordinate];
				}
			}
		}

		private readonly IDictionary<Coordinate, object> _coordDataMap = new SortedDictionary<Coordinate, object>();
	}
}
